package com.qxcy.gzy.util;
import android.os.Environment;

import java.io.File;
import java.io.IOException;

public class FileUtils {

    //检查文件夹，不存在即创建
    public static void createDir(String path) {
        //文件夹检查，不存在则创建
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            //文件夹
            File dir = new File(path);
            if (!dir.exists()) {
                boolean isSuccess = dir.mkdirs();
                System.out.println(isSuccess);
            }
        }
    }

    //创建新文件
    public static File createNewFile(String path, String fileName) {
        File file = new File(path, fileName);
        createDir(path);
        String fullName = path + File.separator + fileName;
        try {
            //存在即删除
            if (fileIsExists(fullName)) {
                boolean isSuccess = file.delete();
                System.out.println(isSuccess);
            }
            //创建
            boolean isSuccess = file.createNewFile();
            System.out.println(isSuccess);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

    //判断文件是否存在
    public static boolean fileIsExists(String strFile) {
        try {
            File f = new File(strFile);
            if (!f.exists()) {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}